from pyeasyaccept.commands.ContainsStringCommand import ContainsStringCommand
from pyeasyaccept.commom.Configuration import Configuration
from pyeasyaccept.script.ParsedLineReader import ParsedLineReader
from pyeasyaccept.script.Script import Script
import unittest
import os
from pythoneasyaccept_test.TestFacade import TestFacade

"""
This class tests the 'containsString' command
@author: Danilo Torres
"""
class ContainsStringCommandTest(unittest.TestCase):
    
    configuration = Configuration.getInstance()
    
    """
    This method sets initial information
    """
    def setUp(self):
        self.FILE_SEPARATOR = Configuration.FILE_SEPARATOR          
        self.root = self.configuration.getProjectRoot()
        
    """
    This method tear down informations
    """
    def tearDown(self):
        pass

    """
    This method runs the successful tests
    """
    def testContainsStringCommand(self):
        testContainsStringCommand1 = self.root + self.configuration.TEST_SCRIPTS_LOCATION + os.sep + "TestContainsString1.txt"        
         
        parsedLineReader = ParsedLineReader(testContainsStringCommand1)
        
         
        testFacade = TestFacade()
        script = Script(testFacade,testContainsStringCommand1)
        
        parsedLine1 = parsedLineReader.getParsedLine()
        parsedLine2 = parsedLineReader.getParsedLine()
        parsedLine3 = parsedLineReader.getParsedLine()
        parsedLine4 = parsedLineReader.getParsedLine()
        
        resultLine1 = script.executeCommand(parsedLine1)
        resultLine2 = script.executeCommand(parsedLine2)
        resultLine3 = script.executeCommand(parsedLine3)
        resultLine4 = script.executeCommand(parsedLine4)
        
        self.assertEquals(Configuration.SUCCESS_CODE, resultLine1.getTestLineStatus())
        self.assertEquals(Configuration.SUCCESS_CODE, resultLine2.getTestLineStatus())
        self.assertEquals(Configuration.SUCCESS_CODE, resultLine3.getTestLineStatus())
        self.assertEquals(Configuration.SUCCESS_CODE, resultLine4.getTestLineStatus())

    """
    This method runs the error tests
    """
    def testContainsStringCommandError(self):
        testContainsStringCommandError = self.root + self.configuration.TEST_SCRIPTS_LOCATION + os.sep + "TestContainsStringError1.txt"        
         
        parsedLineReader = ParsedLineReader(testContainsStringCommandError)
        
        testFacade = TestFacade()
        script = Script(testFacade, testContainsStringCommandError)
        
        parsedLine1 = parsedLineReader.getParsedLine()
        parsedLine2 = parsedLineReader.getParsedLine()
        parsedLine3 = parsedLineReader.getParsedLine()
        parsedLine4 = parsedLineReader.getParsedLine()
        
        resultLine1 = script.executeCommand(parsedLine1)
        resultLine2 = script.executeCommand(parsedLine2)
        resultLine3 = script.executeCommand(parsedLine3)
        resultLine4 = script.executeCommand(parsedLine4)
        
        self.assertEquals(Configuration.ERROR_CODE, resultLine1.getTestLineStatus())
        self.assertEquals(Configuration.ERROR_CODE, resultLine2.getTestLineStatus())
        self.assertEquals(Configuration.ERROR_CODE, resultLine3.getTestLineStatus())
        self.assertEquals(Configuration.ERROR_CODE, resultLine4.getTestLineStatus())
        
        self.assertEquals("The string: \"Python EasyAccept\" doesn't contains: \"error\"", resultLine1.getErrorMessage())
        self.assertEquals("The string: \"Python EasyAccept\" doesn't contains: \"PythonEasyAccept\"", resultLine2.getErrorMessage())
        self.assertEquals("The string: \"Python EasyAccept\" doesn't contains: \" Python \"", resultLine3.getErrorMessage())
        self.assertEquals("The string: \"Python EasyAccept\" doesn't contains: \" EasyAccept \"", resultLine4.getErrorMessage())
        
        
    
if __name__ == '__main__':
    unittest.main() 